package artin.A3D7.main 
{
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.primitives.Plane;
	import alternativa.engine3d.controllers.SimpleObjectController;
	import flash.display.Sprite;
	import flash.events.Event;
	import artin.A3D7.template.AlternativaTemplate;
	import artin.A3D7.serices.DragController;
	import flash.display.DisplayObjectContainer;
	import alternativa.engine3d.core.Object3DContainer;
	import alternativa.engine3d.core.View;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.loaders.MaterialLoader;
	import alternativa.engine3d.loaders.ParserCollada;
	import alternativa.engine3d.objects.Mesh;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Object3DContainer;
	import alternativa.engine3d.core.View;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	/**
	 * Alternativa3D 7.7.0 模板demo
	 * @by ssnangua
	 */
	[SWF(backgroundColor="#000000", frameRate="100", width="800", height="600")]
	public class A3D7LoaderDEA extends Sprite
	{
		private static const PATH_URL:String = "resources/model/tank/";
		private static const COLLADA_URL:String = "tank.dae";
		private var rootContainer:Object3DContainer = new Object3DContainer();
		
		private var dragger:DragController;
		private var userCamera:Object3D = new Object3D();

		private var parser:ParserCollada;
		
		private var loader:URLLoader;
		private var materialLoader:MaterialLoader;
		// 创建模板（AlternativaTemplate）
		private var scene:AlternativaTemplate;
		
		public function A3D7LoaderDEA():void
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			scene= new AlternativaTemplate(this,AlternativaTemplate.BSP,320,230, true)
//			showLogo();
			// 调整摄像头
			// 设置控制器中的摄像头的位置，不能直接设置x、y、z值
			// 可以使用SimpleController.setObjectPosXYZ()方法
			scene.cameraController.setObjectPosXYZ(0,-500,500);
			scene.cameraController.lookAtXYZ(0, 0, 0);
			addChild(scene.camera.diagram);
		
			dragger = new DragController(stage, 0, 0, 100);
			dragger.enabled = true;
			
			
			var v:Vector3D = dragger.position;
			userCamera.x = v.x;
			userCamera.y = v.y;
			userCamera.z = v.z;
			
			loadCollada();

			
			scene.camera.render();
			
		/*	
			
			
			
			// 创建材质（Material）
			var material:FillMaterial = new FillMaterial(0x666666,1,0,0x0);
			
			// 创建平面（Plane）
			var plane:Plane = new Plane(300,300,4,4);
			plane.setMaterialToAllFaces(material);
			scene.container.addChild(plane);
			
			// 调整摄像头
			// 设置控制器中的摄像头的位置，不能直接设置x、y、z值
			// 可以使用SimpleController.setObjectPosXYZ()方法
			scene.cameraController.setObjectPosXYZ(0,-500,500);
			scene.cameraController.lookAtXYZ(0, 0, 0);
			addChild(scene.camera.diagram);
			
			// 创建对象控制器
			// 拖动鼠标是旋转对象
			
			var objectController:SimpleObjectController = new SimpleObjectController(stage,plane,100);
			objectController.unbindAll();
				
			scene.onPreRender = function():void {
				//scene.cameraController.update();
				objectController.update();
			
			};
			*/
			
		}
		
		
		
		private function loadCollada() : void
		{
			loader = new URLLoader();
			loader.load(new URLRequest(PATH_URL+COLLADA_URL));
			loader.addEventListener(Event.COMPLETE, onLoadCollada);
		}
		private function onLoadCollada(e:Event) : void
		{
			//解析模型文件
			parser = new ParserCollada();
			parser.parse(new XML(loader.data), PATH_URL);//注意这个地方，PATH_URL是你要加载的贴图路径
			//导入贴图，如果模型不自带贴图的话，需要另外加载贴图文件
			materialLoader = new MaterialLoader();
			materialLoader.addEventListener(Event.COMPLETE, onCompleteMaterial);
			materialLoader.load(parser.textureMaterials, new LoaderContext(true));
		}
		
		
		private function onCompleteMaterial(e:Event) : void
		{
			//遍历所有模型里的对象
			for each(var o:Object3D in parser.objects) {
				if (!o.name) continue;
				scene.container.addChild(o);
			}
			addEventListener(Event.ENTER_FRAME, onTick);
		}
		private function onTick(e:Event):void
		{
			var v:Vector3D = dragger.position;
			userCamera.x += (v.x - userCamera.x) * 0.3;
			userCamera.y += (v.z - userCamera.y) * 0.3;
			userCamera.z += (v.y - userCamera.z) * 0.3;
			scene.cameraController.setObjectPosXYZ(userCamera.x, userCamera.y, userCamera.z);
			scene.cameraController.lookAtXYZ(0, 0, 0);
			// 开始渲染
			scene.startRendering();
		}


	}
}